'''
模拟 layer1 client

模拟plc变量 {int:[], int:[]}
'''
import time
import traceback

# 导入PLCClientInterface

try:
    from s7_client import S7Client # layer2/...
except:
    from plc_S7.s7_client import S7Client #


class SimClient(S7Client): #
    # 设为静态变量，多个plc对象共享一个data
    client = None  # {int:[], int:[]} #  {dbnum_int: {ads_int: value}}
    def __init__(self, ip='127.0.0.1', port=1102, dbnumber=0):
        # 创建通讯客户端实例
        # self.client = modbus_tcp.TcpMaster(host=ip, port=port, timeout_in_sec=3.0)

        self.ip = ip
        self.port = port
        self.dbnumber = dbnumber # s7 考虑db number
    def connect(self):
        # 连接至PLC
        try:
            # self.client.connect(self.ip, 0, 0, self.port)  # 请求大小为480B的pdu
            # self.client = [0 for _ in range(2000)] # sim变量值
            SimClient.client = {}
            # self.client[self.dbnumber] = [0 for _ in range(1000)] # sim变量值
            SimClient.client[self.dbnumber] = {}  # sim变量值
            print(f'client connect to sim {SimClient.client}')
        except:
            print(traceback.format_exc())
        # print(f"客户端 是否连接到 plc服务端：{self.client.get_connected()}")

    def get_connected(self):
        # return self.client.get_connected()
        return SimClient.client is not None

    def read_int8(self, start=0, size=1, dbnumber=None):
        s7_db_numb = self.dbnumber if dbnumber is None else dbnumber
        if not SimClient.client.get(s7_db_numb):
            # self.client[s7_db_numb] = [0 for _ in range(1000)]
            SimClient.client[s7_db_numb] = {}
        if not SimClient.client[s7_db_numb].get(start):
            SimClient.client[s7_db_numb][start]=0


        return SimClient.client[s7_db_numb][start]

    def read_int16(self, start=0, size=2):  # 读int16
        pass

    def read_bool(self, start=0, size=1): # 读bool
        pass

    def read_real(self, start=0, size=4): # 读实数
        pass

    def write_int8(self, start=0, data_int8=0, dbnumber=None): # 写int8
        s7_db_numb = self.dbnumber if dbnumber is None else dbnumber
        if not SimClient.client.get(s7_db_numb):
            # self.client[s7_db_numb] = [0 for _ in range(1000)]
            SimClient.client[s7_db_numb] = {}


        SimClient.client[s7_db_numb][start] = data_int8
        print(SimClient.client)

    def write_int16(self, start=0, data_int8=0): #
        pass

    def write_real(self, start=0, data_real=1.0): # 写实数  用于发送坐标等
        pass

    def write_bool(self, start=0, data_bool=False): # 写bool
        pass

    def read_string(self, start=0, size=50, dbnumber=None): # 读 string  size=256
        s7_db_numb = self.dbnumber if dbnumber is None else dbnumber

        if not SimClient.client.get(s7_db_numb):
            # self.client[s7_db_numb] = [0 for _ in range(1000)]
            SimClient.client[s7_db_numb] = {}
        if not SimClient.client[s7_db_numb].get(start):
            SimClient.client[s7_db_numb][start]= 'sim_default_value'

        return SimClient.client[s7_db_numb][start] # todo dbnumber

    def write_string(self, start=0, data_str='hello', dbnumber=None): # write string
        s7_db_numb = self.dbnumber if dbnumber is None else dbnumber
        if not SimClient.client.get(s7_db_numb):
            # self.client[s7_db_numb] = [0 for _ in range(1000)]
            SimClient.client[s7_db_numb] = {}

        SimClient.client[s7_db_numb][start] = data_str

    def close(self):
        SimClient.client= None
        # print(f"plc 连接状态：{self.client.get_connected()}")


def test_SimClient():
    ly1_client = SimClient()
    ly1_client.connect()
    ly1_client.write_int8(start=10, data_int8=100)
    ly1_client.write_int8(start=12, data_int8=121)
    value = ly1_client.read_int8(start=10)
    print(value)
    ly1_client.close()

if __name__ == "__main__":
    test_SimClient()